package Review;

public class Sort {
    public static void selectSort(int[] array){
        // write code  here
        for (int i=0;i<array.length;i++){
            int minIndex = i;
            for (int j=i+1;j<array.length;j++){
                if (array[j]<array[minIndex]){
                    minIndex =j;
                }
            }
            swap(array,i,minIndex);
        }
    }


// 堆排序
public static void heapSort(int[] array){
        // write code  here
    creatHeap(array);
    int end =array.length-1;
    while(end>0){
        swap(array,0,end);
        siftDown(array,0,end);
        end--;
    }
    }
    private static void creatHeap(int[] array){
        for (int parent = (array.length-1-1)/2;parent>=0;parent--){
            siftDown(array,parent,array.length);
        }
    }

    private static void siftDown(int[] array, int parent, int length) {
        int child = 2*parent+1;
        while (child<array.length){
            if (child+1<array.length&&array[child]<array[child+1]){
                child++;
            }
            if (array[child]>array[parent]){
                swap(array,parent,child);
                parent =child;
                child = 2*parent+1;
            }
        }
    }

    public static void swap(int[] arr,int i,int j){
        int tmp = arr[i];
        arr[i] =arr[j];
        arr[j] = tmp;
    }
    // 冒泡排序
public static void bubbleSort(int[] array){
        // write code  here
    for (int i =0;i<array.length-1;i++){
        boolean flg =false;
        for (int j=i+1;j<array.length-1-i;j++){
            if (array[j+1]<array[j]){
                swap(array,j,j+1);
                flg =true;
            }
        }
        if (!flg){
            break;
        }
    }
    }

// 快速
public static void quickSort(int[] array){
        // write code  here
    quick(array,0,array.length-1);

    }
    private  static void  quick(int[] array,int start,int end){
        if(start>=end){
            return;
        }
        int minIndex = gerMiddleNum(array,start,end);
        swap(array,start,minIndex);
        int pivot = partiton(array,start,end);
        quick(array,start,pivot-1);
        quick(array,pivot+1,end);
    }

    private static int partiton(int[] array, int left, int right) {
        int tmp = array[left];
        while (left<right){
            while (left<right&&array[right]>=tmp){
                right--;
            }
            array[left] = array[right];
            while (left<right&&array[left]<=tmp){
                left++;
            }
           array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    private static int gerMiddleNum(int[] array, int left, int right) {
        int mid =(left+right)/2;
        if (array[left]<array[right]) {
                if (array[left]<array[right]){
                    return left;
                } else if (array[mid]>array[right]) {
                    return right;
                }else {
                    return mid;
                }
        }else{
            if (array[mid]>array[left]){
                return left;
            } else if (array[mid]<array[right]) {
                return right;
            }else{
                return mid;
            }
        }
    }
}

